<template>
  <div class="order-list-container">
    <el-breadcrumb>
      <el-breadcrumb-item>房型管理</el-breadcrumb-item>
      <el-breadcrumb-item>损坏物件设置</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="easy-content">
      <el-form>
        <el-form-item>
          <div class="order-warpper">
            <div class="group-2">
              <el-dropdown @command="setState">
                <span class="el-dropdown-link">
                  {{stateStr}}
                  <i class="el-icon-arrow-down el-icon--right"></i>
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item :command="{title:'全部状态',id:''}">全部状态</el-dropdown-item>
                  <el-dropdown-item :command="{title:'上架',id:'1'}">上架</el-dropdown-item>
                  <el-dropdown-item :command="{title:'下架',id:'2'}">下架</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </div>
            <div class="group-1">
              <input autocomplete="off"
                placeholder="请输入物品名称/拼音/编号"
                type="text"
                rows="2"
                validateevent="true"
                class="el-input__inner"
                v-model="search">
            </div>
            <el-button type="primary"
              @click.native="_getBreakGoodsList"
              class="searc"
              style=" line-height: 32px;padding: 0 21px;">
              <i class="el-icon-search"></i>
            </el-button>
            <el-button type="primary"
              class="searc"
              style=" line-height: 32px;padding: 0 21px;float:right"
              @click.native="add" v-has="'damageaddchange'">
              新增物品
            </el-button>
          </div>
        </el-form-item>
        <el-form-item class="easy-table">
          <el-table :data="breakGoodsList"
            style="width: 100%"
            max-height="600"
            >
            <el-table-column prop="name"
              label="物品名称"
              min-width="100">
            </el-table-column>
             <el-table-column prop="spell"
              label="物品拼音"
              min-width="100">
            </el-table-column>
            <el-table-column prop="number"
              label="物品编号"
              min-width="100">
            </el-table-column>
            <el-table-column prop="picture"
              label="图片"
              min-width="100">
              <template slot-scope="scope">
                <img :src="scope.row.picture"
                  alt=""
                  width="30%"
                  style="display:block">
              </template>
            </el-table-column>
            <el-table-column prop="money"
              label="赔偿价格"
              min-width="100">
            </el-table-column>
            <el-table-column prop="type"
              label="型号"
              min-width="100">
            </el-table-column>
            <el-table-column prop="status"
              label="状态"
              min-width="100">
              <template slot-scope="scope">
                <span>{{scope.row.status == 1? '正常':'失效'}}</span>
              </template>
            </el-table-column>
            <el-table-column prop="updateTime"
              label="最新上架时间"
              min-width="100">
            </el-table-column>
            <el-table-column fixed="right"
              label="操作"
              width="200">
              <template slot-scope="scope">
                <el-button type="text"
                  size="small"
                  @click="editBreak(scope.row)" v-has="'damageaddchange'">编辑</el-button>
                <el-button type="text"
                  size="small"
                  :disabled="scope.row.status == 1"
                  @click.native="editBreakState(scope.row)" v-has="'damagestatus'">上架</el-button>
                <el-button type="text"
                  size="small"
                  :disabled="scope.row.status == 2"
                  @click.native="editBreakState(scope.row)" v-has="'damagestatus'">下架</el-button>
                <el-button type="text"
                  size="small"
                  @click="deleteBreak(scope.row.id)"  v-has="'damagedelete'">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
        <el-form-item>
          <el-pagination @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageNo"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="pageSize"
            layout="total, prev , pager , next, sizes"
            :total="total"
            style="margin-top:20px">
          </el-pagination>
        </el-form-item>
      </el-form>

      <el-dialog width="400px"
        :title="title"
        :visible.sync="update_popule"
        @close="_rest('updateData')">
        <el-form :rules="rules"
          ref="updateData"
          :model="updateData"
          label-width="80px">
          <el-form-item label="物品编号"
            prop="number">
            <el-input type="text"
              v-model="updateData.number"
              placeholder="请输入物品编号"
              maxlength="8"
              @keyup.native="updateData.number = formatInput(updateData.number)"></el-input>
          </el-form-item>
          <el-form-item label="物品名称"
            prop="name">
            <el-input type="text"
              v-model="updateData.name"
              placeholder="请输入物品名称"
              maxlength="16"
              @keyup.native="updateData.name = formatInput(updateData.name)"></el-input>
          </el-form-item>
          <el-form-item label="物品拼音"
            prop="spell"
            required>
            <el-input type="text"
              v-model="updateData.spell"
              placeholder="请输入物品名称拼音"
              maxlength="16"></el-input>
          </el-form-item>
          <el-form-item label="物品图片">
            <el-upload class="avatar-uploader"
              :show-file-list="false"
               action="https://jsonplaceholder.typicode.com/posts/"
              :http-request="sendChange">
              <img v-if="updateData.picture"
                :src="updateData.picture"
                class="avatar"
                width="88"
                height="88" />
              <i v-else
                class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="物品型号"
            prop="type">
            <el-input type="text"
              v-model="updateData.type"
              placeholder="请输入物品型号"
              maxlength="8"
              @keyup.native="updateData.type = formatInput(updateData.type)"></el-input>
          </el-form-item>
          <el-form-item label="物品价格"
            prop="money"
            required>
            <el-input type="text"
              v-model="updateData.money"
              placeholder="请输入物品价格"
              maxlength="4"></el-input>
          </el-form-item>
          <el-form-item label="单位"
            prop="unit">
            <el-input type="text"
              v-model="updateData.unit"
              placeholder="请输入物品单位"
              maxlength="3"
              @keyup.native="updateData.unit = formatInput(updateData.unit)"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer"
          class="dialog-footer">
          <el-button type="primary"
            @click="_updateBreakGood('updateData')">确 定</el-button>
          <el-button @click="update_popule = false">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
const Breadcrumb = () => import("@/components/common/Breadcrumb");
import {
  getBreakGoodsList,
  deleteBreakGood,
  updateBreakGood,
  updateBreakGoodState,
  upimg
} from "@/public/js/setting.js";
import { formatInput } from "@/public/js/util";
export default {
  components: {
    Breadcrumb
  },
  data() {
    var validateEveryPrice = (rule, value, callback) => {
      let re = /[^\d.]/g;
      if (value == "" || value == null) {
        callback(new Error("请输入物品价格"));
      } else if (re.test(value)) {
        callback(new Error("请您输入正确的格式"));
      } else {
        callback();
      }
    };
    //
    var validateEverySpell = (rule, value, callback) => {
      let re = /^[A-Za-z]+$/;
      if (value == "") {
        callback(new Error("请输入物品拼音"));
      } else if (!re.test(value)) {
        callback(new Error("请您输入正确的格式"));
      } else {
        callback();
      }
    };
    return {
      content: {
        firstTitle: "设置",
        secondTitle: "损坏管理",
        type: "close"
      },
      roomStatusHueList: [],
      Pouple_name: false,
      staffStatusName: "",
      colorData: {},
      rules: {
        number: [
          { required: true, message: "请输入物品编号", trigger: "blur" }
        ],
        name: [{ required: true, message: "请输入物品名称", trigger: "blur" }],
        spell: [{ validator: validateEverySpell, trigger: "blur" }],
        type: [{ required: true, message: "请输入物品型号", trigger: "blur" }],
        money: [{ validator: validateEveryPrice, trigger: "blur" }],
        unit: [{ required: true, message: "请输入物品单位", trigger: "blur" }]
      },
      pageNo: 1,
      pageSize: 10,
      search: "",
      status: "",
      total: 0,
      breakGoodsList: [],
      stateStr: "物品状态",
      title: "新增物品",
      updateData: {
        id: "",
        name: "",
        spell: "",
        number: "",
        picture: "",
        money: "",
        unit: "",
        type: ""
      },
      update_popule: false,
      handleType: 0,
      formatInput
    };
  },
  created() {
    this._getBreakGoodsList();
  },

  mounted() {},
  methods: {
    _getBreakGoodsList() {
      getBreakGoodsList({
        pageNo: this.pageNo,
        pageSize: this.pageSize,
        search: this.search,
        status: this.status
      }).then(res => {
        this.breakGoodsList = res.data.damageList;
        this.total = res.data.total;
      });
    },
    // 状态筛选
    setState(val) {
      console.log(val);
      this.status = val.id;
      this.stateStr = val.title;
      this._getBreakGoodsList();
    },
    //编辑
    editBreak(data) {
      this.updateData = {
        id: data.id,
        name: data.name,
        spell: data.spell,
        number: data.number,
        picture: data.picture,
        money: data.money,
        unit: data.unit,
        type: data.type
      };
      this.update_popule = true;
      this.title = "编辑物品";
      this.handleType = 1;
    },
    // 分页
    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this._getBreakGoodsList();
    },
    handleCurrentChange(pageNo) {
      this.pageNo = pageNo;
      this._getBreakGoodsList();
    },
    _rest(formName) {
      this.updateData = {
        id: "",
        name: "",
        spell: "",
        number: "",
        picture: "",
        money: "",
        unit: "",
        type: ""
      };
      this.$refs[formName].resetFields();
    },
    // 增加
    add() {
      this.update_popule = true;
      this.title = "增加物品";
      this.handleType = 0;
    },
    // 编辑状态
    editBreakState(row) {
      let status = row.status == 1 ? 2 : 1;
      let statusInfo = row.status == 1 ? "下架" : "上架";
      this.$confirm(`是否${statusInfo}该物品`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          updateBreakGoodState({
            id: row.id,
            status: status
          }).then(res => {
            if (!res.data.errCode) {
              this.$message({
                type: "success",
                message: "操作成功!"
              });
            }
            this._getBreakGoodsList();
          });
        })
        .catch(() => {});
    },
    //删除
    deleteBreak(id) {
      this.$confirm("此操作将永久删除该物品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deleteBreakGood({
            id: id,
            rmStatus: 2
          }).then(res => {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this._getBreakGoodsList();
          });
        })
        .catch(() => {});
    },
    // 新增修改
    _updateBreakGood(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          updateBreakGood(this.updateData).then(res => {
            if (!res.data.errCode) {
              this.$message({
                type: "success",
                message: this.handleType ? "新增成功" : "修改成功"
              });
              this.$refs[formName].resetFields();
              this.update_popule = false;
              this._getBreakGoodsList();
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 上传图片
    sendChange(e) {
      var reader = new FileReader();
      reader.readAsDataURL(e.file);
      reader.onload = e => {
        let photoPath = e.target.result;
        upimg({
          nowPath: photoPath,
          oldPath: this.updateData.picture
        }).then(res => {
          this.updateData.picture = res.data.path;
        });
      };
    }
  }
};
</script>
<style lang="scss" scoped>
.order-warpper {
  .group-1 {
    width: 300px;
    display: inline-block;
    margin-right: 20px;
  }
  .group-2 {
    width: 80px;
    display: inline-block;
    margin-right: 20px;
  }
  .group-3 {
    margin-top: 20px;
  }
  .btn {
    text-align: right;
  }
  .el-input__inner {
    height: 34px;
  }
  .search-button .el-button {
    padding: 9px 20px;
  }
}
.order-list-container {
  text-align: left;

  .el-date-editor--daterange.el-input__inner {
    padding: 0;
    padding-left: 10px;
    width: auto;
  }

  .el-input {
    width: auto;
  }

  .easy-search-other {
    width: 300px;
  }
}
.account-content {
  & > div {
    display: flex;
    padding-bottom: 15px;
    & > div {
      flex: 1;
    }
  }
}
.miNiCommListTable {
  border: 1px solid #ddd;
  border-right: 0;
  li {
    display: flex;
    border-bottom: 1px solid #ddd;

    div {
      flex: 1;
      line-height: 45px;
      font-size: 12px;
      text-indent: 23px;
      border-right: 1px solid #ddd;
    }
    &.header > div {
      background: #eef1f9;
      color: #415161;
    }
    & > .input-number .input-number-count {
      text-align: center;
      text-indent: 0;

      p {
        display: inline-block;
        background: #6d2377;
        height: 30px;
        line-height: 30px;
        width: 30px;
        border-radius: 5px 0 0 5px;
        color: #fff;
        cursor: pointer;
      }
      input {
        display: inline-block;
        height: 30px;
        line-height: 30px;
        border: none;
        width: 30px;
        text-align: center;
        border: 1px solid #6d2377;
        box-sizing: border-box;
      }
    }
  }
}
.handleBtn {
  float: left;
  height: 47px;
  line-height: 47px;
  padding-left: 15px;
  color: #409eff;
}
</style>


